home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1160 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help with gettng 2 chars into an integer!
  5. Date: 11 Jan 1996 20:00:08 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan11130008@qcd.lanl.gov>
  8. References: <4d2eh1$7pm@usc.edu> <4d3e2u$1hr@crl.crl.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: bobfry@crl.com's message of 11 Jan 1996 08:32:30 -0800
  13.  
  14. --text follows this line--
  15. In article <4d3e2u$1hr@crl.crl.com> bobfry@crl.com (Robert Fry) writes:
  16. <snip>
  17.    >Hi. I can't seem to figure out how to do this. Basically I have to
  18. chars that  
  19.    >I want to put into a 2-byte integer. It seems easy at first, but I
  20. can't seem  
  21.    >to figure out to do it with the bit-fidling operators (shifting or
  22. masking).  
  23.    >Simple put, I'd like to do:
  24.  
  25.    >    char a,b;
  26.    >    int c;
  27.  
  28.    >    a = 'A';
  29.    >    b = 'B';
  30.    >    c = ? /* the first byte in c should contain the value of a, and the 
  31.    >second byte should contain the value of b */
  32.  
  33.    Assuming chars are 8 bits, and assuming sizeof( int) >= 2 * sizeof( char),
  34.    then you can use the following (note that it may be incorrect with 
  35.    respect to the endianness of your CPU, and is clearly not portable):
  36.  
  37.    c = (( a << 8) & 0x0FF00) | ( b & 0x00FF);
  38.  
  39.    This will work. You can also put a and b into a union that contains an 
  40.    array of 2 characters and an int. I'm not sure how to make the structure 
  41.    independent of endianness, but someone should have an idea if that's an 
  42.    issue for you.
  43.  
  44.    I've used both, depending on the needs of the moment.
  45.  
  46. There have been a number of answers to the quetsion, I am choosing to
  47. follow up on this because only this post stated it assumptions
  48. correctly. One of the assumptions could be waekened by using CHAR_BIT
  49. instead of 8; whether that is meaningful depends on whether the
  50. original poster meant `byte' to be an arbitrary length capable of
  51. holding a char: as C defines it, or an octet as often common in
  52. networking or binary file format descriptions.
  53.  
  54. The reason for this follow up is, however, to add a note about the use
  55. of unions for this purpose. Strictly speaking, once you store
  56. something into one member of an union, accessing a different member
  57. has implementation defined behaviour. Hence, an implementation may
  58. specify aggressive optimization in which it defines the behaviour to
  59. be undefined. It is unlikely that an implementation will choose to do
  60. so by default (on marketing grounds), but it is allowed. Similarly
  61. byte order issues (and `holes' in integral types), are, by this rule,
  62. to be addressed by the individual implementation; and, no portable
  63. solution is possible.
  64.  
  65. Having said that, I must admit that most often when one needs to do
  66. things like this, one is probably not looking for absolute portability
  67. anyway. I have often used unions and structs with bitfields even for
  68. non-portable, but fast and readable code.
  69.  
  70. Cheers
  71. Tanmoy
  72.  
  73.  
  74. --
  75. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  76. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  77. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  78. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  79. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  80. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  81.